﻿using System;
using System.Configuration;
using System.IO;
using NsbExtensions.Configuration;
using NsbExtensions.Endpoint.Management.Monitoring;
using NsbExtensions.Messages.Management.Monitoring;
using NsbExtensions.Server.Configuration;
using NsbExtensions.Tests.Stubs;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace NsbExtensions.Tests.Config
{
    [TestFixture]
    public class ConfigurationTestFixture
    {
        private System.Configuration.Configuration GetTestConfiguration()
        {
            var map = new ExeConfigurationFileMap
                          {
                              ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestApp.config")
                          };

            return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        }

        [Test]
        public void EndpointConfigurationSectionAndAssemblyNameConfig()
        {
            System.Configuration.Configuration cfg = GetTestConfiguration();
            Assert.That(cfg, SyntaxHelper.Not.Null);

            var epConfig = cfg.GetSection("endpoint") as EndpointConfiguration;
            Assert.That(epConfig, SyntaxHelper.Not.Null);
            Assert.That(epConfig.EndpointID, Is.EqualTo("myEndPoint"));
            Assert.That(epConfig.Name, Is.EqualTo("ThisIsMyNameDontWearItOut"));
            Assert.That(epConfig.MessageBusAddress, Is.EqualTo("myEndPoint"));
            Assert.That(epConfig.EndpointLatitude, Is.EqualTo(-121.34));
            Assert.That(epConfig.EndpointLongitude, Is.EqualTo(32.432));
            Assert.That(epConfig.MessageHandlerAssemblies.Count, Is.EqualTo(1));
            Assert.That(epConfig.MessageHandlerAssemblies[0].Assembly, Is.EqualTo(typeof (MessagingEndpoint).Assembly));
        }

        [Test]
        public void EndpointManagerConfig()
        {
            System.Configuration.Configuration cfg = GetTestConfiguration();

            var epMgr = cfg.GetSection("endpointManager") as EndpointManagerConfiguration;
            Assert.That(epMgr, SyntaxHelper.Not.Null);

            Assert.That(epMgr.StatusReportInterval, Is.EqualTo(TimeSpan.FromMinutes(5)));
            Assert.That(epMgr.StatusReportGracePeriod, Is.EqualTo(TimeSpan.FromMinutes(1)));
            Assert.That(epMgr.MonitorServiceAddress, Is.EqualTo("EndpointMonitorService"));

            Assert.That(epMgr.StatusProbes.Count, Is.EqualTo(2));

            IEndpointStatusProbe p1 = epMgr.StatusProbes[0].CreateProbeFromConfiguration();
            Assert.That(p1, SyntaxHelper.Not.Null);
            Assert.That(p1.Name, Is.EqualTo("myProbe"));
            Assert.That(p1.UnitOfMeasurement, Is.EqualTo("TestUnit"));
            Assert.That(p1.DataType, Is.EqualTo(MeasurementDataType.Text));
            Assert.That(((TestStatusProbe) p1).TestCustomProperty, Is.EqualTo(DateTime.Parse("08/04/1976 3:32 AM")));
            Assert.That(((TestStatusProbe) p1).TestCustomProperty2, Is.EqualTo("HaHa I Am A Test"));

            var p2 = epMgr.StatusProbes[1].CreateProbeFromConfiguration() as PerfCounterProbe;
            Assert.That(p2, SyntaxHelper.Not.Null);
            Assert.That(p2.Category, Is.EqualTo("Memory"));
            Assert.That(p2.Counter, Is.EqualTo("Available MBytes"));
            Assert.That(p2.Instance, Is.EqualTo(String.Empty));
            Assert.That(Double.Parse(p2.TakeMeasurement(null).Value), Is.GreaterThan(0.0));
        }

        [Test]
        public void MonitorServerConfig()
        {
            System.Configuration.Configuration cgf = GetTestConfiguration();

            var scfg = cgf.GetSection("endpointMonitorServer") as EndpointMonitorConfiguration;
            Assert.That(scfg, SyntaxHelper.Not.Null);
            Assert.That(scfg.StatusBroadcastInterval, Is.EqualTo(TimeSpan.FromMinutes(1)));
            Assert.That(scfg.EndpointMonitorInterval, Is.EqualTo(TimeSpan.FromMinutes(1)));
        }
    }
}